package org.keyyond.ms.business.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.annotation.JsonAutoDetect;

/**
 * @author YCR
 * @belong to Shccs
 * @date:2018年1月19日
 * @date:2018年1月19日
 * @version : V1.0
 */
@Configuration
public class RedisConfig {

    /**
     * 配置redis服务
     * @author YCR
     * @param
     * @return RedisTemplate
     * @throws
     */
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
	// 创建一个模板类
	RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
	// 将刚才的redis连接工厂设置到模板类中
	template.setConnectionFactory(factory);
	// 设置key的序列化器
	template.setKeySerializer(new StringRedisSerializer());
	// 设置value的序列化器
	// 使用Jackson 2，将对象序列化为JSON
	Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
	// json转对象类，不设置默认的会将json转成hashmap
	ObjectMapper om = new ObjectMapper();
	om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
	om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
	jackson2JsonRedisSerializer.setObjectMapper(om);
	template.setValueSerializer(jackson2JsonRedisSerializer);

	return template;
    }

}
